package extractor;

import java.io.IOException;
import java.util.HashMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class CategoryHelper {
	HashMap<String, String[]> categoryMap = new HashMap<String, String[]>();
	public void init(FileSystem fs, Path path, Configuration conf) throws IOException{
		categoryMap.clear();
		FileStatus[] srcs = fs.globStatus(path);
		for (int i = 0; i < srcs.length; ++i) {
			if (srcs[i].isDir())
				continue;
			Path p = srcs[i].getPath();			
			SequenceFile.Reader reader = new SequenceFile.Reader(fs, p, conf);
			BytesWritable key = new BytesWritable();
			Text value = new Text();
			while(reader.next(key, value)) {
				String[] values = value.toString().split("\u0001");
				categoryMap.put(values[0], values);
			}
			reader.close();		
		}
	}
	
	public String[] getCategoryInfo(String category) {
		if (categoryMap.containsKey(category))
			return categoryMap.get(category);
		return null;
	}
}
